/**
 * Created by Administrator on 17-10-12.
 */
var  room=require('./room_d89');
var     P = require("bluebird");
var  iconst=require('./iconst');

room.prototype.userReady=function(uid)
{
    console.logr(this.roomPsw,'userReady',uid);
    var  userInfo=this.getUserInfo(uid);
    var  self=this;
    //如果玩家已在别的房间的话
    if(userInfo.gamingRoomInfo!=null&&userInfo.gamingRoomInfo.roomId!=this.roomPsw)
    {
        console.logr(self.roomPsw,'玩家已在别的游戏房间中不能在本房间开始游戏',uid,self.roomPsw,userInfo.gamingRoomInfo);
        var  msg={event:iconst.event.error,msg:'已在别的游戏房间中',gamingRoomInfo:userInfo.gamingRoomInfo};
        this.sendByUid(uid,msg);
        return;
    }

    var user=this.getUser(uid);
    if(user.playing==1)
    {   console.logr(this.roomPsw,'玩家已准备',uid);
        return;
    }
    var gamingCount=this.get('gamingCount');
    var gameCount=this.get('gameCount');

    if(gamingCount>=gameCount)
    {
        console.logr(this.roomPsw,'游戏已经结束不能再点击了',uid);
        return;
    }
    var  gameStatus=this.get('gameStatus');
    if(gameStatus!='waitReady'&&gameStatus!='onIdle')
    {   console.logr(this.roomPsw,'已经发牌了不能加入游戏',uid,gameStatus);
        this.sendByUid(uid,{event:iconst.event.error,msg:'开始发牌了不能加入游戏'});
        return;
    }
    if(user.gaming==0)
        this.recordUserRoomInfo(uid);
    user.gaming=1;
    user.playing=1;
    user.bet=0;
    user.zbet=0;
    var  list=this.get('userList');
    var  count=0;
    var  len=list.length;
    for(var i=0;i<len;i++)
    {
        var  u=list[i];
        if(u.playing==1)
        { count++;}
    }
    var  gameType=this.get("gameType");
    var  userMap=this.get("userMap");
    if(gameType==iconst.d89.gameTypeGudingZhuangjia)
    {
        var  bank=userMap[this.get("ownerUid")];
        bank.zbet=1;
        if(bank&&bank.playing!=1)
        {   console.logr(this.roomPsw,'固定庄家模式，庄家没有准备游戏不能开始');
            //this.changeState('waitReady');
            this.notifyUpdate();
            return;}
    }
    if(count==2)
    {   console.logr(this.roomPsw,'2个人已经开始准备,切换状态');
        this.changeState('waitReady');
    }
    if(count==len)
    {
        console.logr(this.roomPsw,'进入等待下注环节');
        this.waitReadyEnd();
    }
    this.notifyUpdate();
}

room.prototype.userQiangzhuang=function(uid,data)
{   console.logr(this.roomPsw,'玩家抢庄',uid,data.zbet);
    var user=this.getUser(uid);
    if(user.playing==0)
    {   console.logr(this.roomPsw,'玩家当前状态不能抢庄',uid);
        return;
    }
    if(user.zbet>0)
    {   console.logr(this.roomPsw,'不能重复抢庄',uid);
        return;}
    var  zbet=data.zbet;
    if(zbet==1||zbet==2||zbet==4||zbet==-1)
    { user.zbet=zbet; }
    else
    {  user.zbet=1;}
    var  count=0;
    var  m=0;
    var  list=this.get('userList');
    var  len=list.length;
    var  qiangzhuangList=[];
    for(var i=0;i<len;i++)
    {   var user=list[i];
        if(user.playing==1)
        {m++;
            if(user.zbet!=0)
            {count++;var it={uid:user.uid,zbet:user.zbet};qiangzhuangList.push(it);}
        }
    }
    this.extraData.qiangzhuangList=qiangzhuangList;
    //抢庄结束了
    if(m==count)
    {
        console.logr(this.roomPsw,'抢庄结束');
        this.waitQiangzhuangEnd();
        return;
    }
    this.notifyUpdate();
}
room.prototype.userChooseBet=function(uid,data)
{
    console.logr(this.roomPsw,'userChooseBet',uid,data.bet);
    var user=this.getUser(uid);
    if(user.playing==0)
    {   console.logr(this.roomPsw,'玩家当前状态不能下注',uid);
        return;
    }
    if(user.bet>0)
    {   console.logr(this.roomPsw,'不能重复下注',uid);
        return;
    }
    user.bet=data.bet;
    console.logr(this.roomPsw,'玩家下注',uid,user.bet);
    var  list=this.get('userList');
    var  count=0;
    var  len=list.length;
    var  m=0;
    for(var i=0;i<len;i++)
    {   var  u=list[i];
        if(u.playing==1&& (u.bet>0))
        { count++;}
        if(u.playing==1) m++;
    }
    this.notifyUpdate();
    if(count>=m-1)
    {    console.logr(this.roomPsw,'所有玩家都下注了,开出小局结算');
        this.changeState('waitTanpai');
        this.notifyUpdate();
        //this.gameOver();
    }
}
room.prototype.userTanpai=function(uid,data)
{   var user=this.getUser(uid);
    if(user.playing==0)
    {return;}
    var  gameStatus=this.get("gameStatus");
    if(gameStatus!="waitTanpai")
        return;
    user.tanpai=1;
    this.extraData={event:"tanpai",uid:uid};
    var  n1=0;
    var   n2=0;
    var  list=this.get("userList");
    var  len=list.length;
    for(var i=0;i<len;i++)
    {
        user=list[i];
        if(user.gaming==1&&user.playing==1)
        {
            n1++; if(user.tanpai==1) n2++;
        }
    }
    if(n1==n2)
    {
        this.changeState('showGameResult');
        this.notifyUpdate();
        this.gameOver();
        return;
    }
    this.notifyUpdate();
}

room.prototype.userCuopai=function(uid,data)
{
    var user=this.getUser(uid);
    if(user.playing==0||user.cuopai==1)
    {return;}
    user.cuopai=1;
    var  cards=user.cards;
    var  msg={event:iconst.event.privateInfo,extraData:{event:iconst.event.cuopai,cid:cards[1].cid}};
    this.sendByUid(uid,msg);

}
room.prototype.userChat=function(uid,data)
{
    var  type=data.type;
    var  content=data.content;
    this.extraData={event:iconst.event.userTalk,type:type,content:content,uid:uid};
    this.notifyUpdate();
}
